package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxOneDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.BoxOne;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxOneService {

    private DatabaseHelper dbHelper;
    private BoxOneDao<BoxOne> BoxOneDao;
    private ArrayList<BoxOne> listBoxOne = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxOneService() {
    }

    public BoxOneService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }


    /**获取单个数据
     * @param BoxOne
     * @return
     */
    public List<BoxOne> getBoxOne(BoxOne BoxOne) {
        BoxOneDao = BaseDaoFactory.getInstance().createBaseDao(BoxOneDao.class, BoxOne.class);
        listBoxOne = new ArrayList<>();
        listBoxOne.addAll(BoxOneDao.query(BoxOne));
        return listBoxOne;
    }

    /**分页获取数据
     * @param BoxOne
     * @return
     */
    public List<BoxOne> getBoxOneList(BoxOne BoxOne,Integer startIndex,Integer limit) {
        BoxOneDao = BaseDaoFactory.getInstance().createBaseDao(BoxOneDao.class, BoxOne.class);
        listBoxOne = new ArrayList<>();
        listBoxOne.addAll(BoxOneDao.query(BoxOne,null,"create_time desc",null,startIndex,limit));
        return listBoxOne;
    }


    public boolean save(BoxOne BoxOne) {
        BoxOneDao = BaseDaoFactory.getInstance().createBaseDao(BoxOneDao.class, BoxOne.class);
        long size = BoxOneDao.insert(BoxOne);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxOne BoxOne) {
        BoxOneDao = BaseDaoFactory.getInstance().createBaseDao(BoxOneDao.class, BoxOne.class);
        BoxOne boxOne = new BoxOne();
        boxOne.setItemId(BoxOne.getItemId());
        List<BoxOne> lis =BoxOneDao.query(boxOne);
        Boolean bl = true;
        if(lis.size()>0){
            BoxOne.setUpdateTime(DateUtils.parseDate());
            long size = BoxOneDao.update(BoxOne,new BoxOne());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            BoxOne.setCreateTime(DateUtils.parseDate());
            long size = BoxOneDao.insert(BoxOne);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxOne BoxOne,BoxOne where) {
        long size = BoxOneDao.update(BoxOne,where);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean delete(BoxOne BoxOne) {
        int size = BoxOneDao.delete(BoxOne);
        if(size >0 ){
            return true;
        }
        return false;
    }

}
